package com.youbug.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youbug.common.vo.CommonTreeVo;
import com.youbug.common.vo.R;
import com.youbug.system.entity.Announcement;
import com.youbug.system.entity.User;
import com.youbug.system.service.IAnnouncementService;
import com.youbug.system.service.ICommonService;
import com.youbug.system.service.IUserService;
import com.youbug.system.vo.AnnouncementVo;
import com.youbug.system.vo.PwdVo;
import com.youbug.system.vo.StatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 通用查询前端控制器
 * </p>
 *
 * @author youbug
 * @since 2023-02-12
 */
@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private ICommonService commonService;
    @Autowired
    private IAnnouncementService announcementService;
    @Autowired
    private IUserService userService;

    @GetMapping("/getAllOrgTrees")
    public R<List<CommonTreeVo>> getDicTypes(){

        List<CommonTreeVo> orgTree = commonService.getOrgTree();

        return R.ok(orgTree);
    }

    @PostMapping("/getAnsmts")
    public R<IPage<Announcement>> getAnsmts(@RequestBody AnnouncementVo vo){

        IPage<Announcement> ansmtPage = announcementService.getAnnouncementPage(vo);

        return R.ok(ansmtPage);
    }

    @PostMapping("/upload/{type}")
    public R<String> uploadFile(MultipartFile file, @PathVariable String type){

        String fileUrl = commonService.saveFile(file,type);

        return R.ok(fileUrl);
    }

    @GetMapping("/getUser/{id}")
    public R<User> getUser(@PathVariable String  id){

        User user = userService.getById(id);

        return R.ok(user);
    }

    @PostMapping("/updateUser")
    public R<Boolean> updateUser(@RequestBody User user){

        boolean save = userService.updateById(user);

        return  R.ok(save);
    }

    @PostMapping("/updatePassword")
    public R<Boolean> updatePassword(@RequestBody PwdVo pwdVo){

        boolean save = userService.updatePassword(pwdVo);

        return  R.ok(save);
    }

    @GetMapping("/getStatistics")
    public R<StatisticsVo> getStatistics(){

        StatisticsVo vo = commonService.getStatistics();

        return  R.ok(vo);
    }
}

